package com.ecode.ocr.controller;

import com.ecode.ocr.domain.business.BusinessResultDTO;
import com.ecode.ocr.domain.idCard.IdCardResultDTO;
import com.ecode.ocr.domain.invoice.InvoiceResultDTO;
import com.ecode.ocr.service.JdCloudService;
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ecode
 */
@Api("ocr识别")
@RestController
@RequestMapping("/ocr")
public class OcrApiController {

    @Resource
    JdCloudService jdCloudService;

    @ApiOperation("身份证识别")
    @PostMapping("ocr-idCard")
    public List<IdCardResultDTO> getIdCardInfo(MultipartFile file) throws Exception {
        List<IdCardResultDTO> idCardResultDTOS = jdCloudService.getIdCardInfo(file);
        return idCardResultDTOS;
    }

    @ApiOperation("营业执照识别")
    @PostMapping("ocr-business")
    public BusinessResultDTO getBusinessInfo(MultipartFile file) throws Exception {
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
        Gson gson = gsonBuilder.create();
        BusinessResultDTO businessInfo = gson.fromJson(gson.toJson(jdCloudService.getBusinessInfo(file)),BusinessResultDTO.class);
        return businessInfo;
    }

    @ApiOperation("增值税发票识别")
    @PostMapping("ocr-invoice")
    public InvoiceResultDTO getInvoiceInfo(MultipartFile file) throws Exception {
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
        Gson gson = gsonBuilder.create();
        InvoiceResultDTO businessInfo = gson.fromJson(gson.toJson(jdCloudService.getInvoiceInfo(file)),InvoiceResultDTO.class);
        return businessInfo;
    }

}
